import BaseNode from './BaseNode'
import Priority from './assist/Priority'

export default class Node extends BaseNode {
    constructor(data) {
        super(data);
        this.offset = [0, 0]; //用来记录节点左右两侧需要偏移的距离;
        this.renderAssist();
    }
    //渲染优先级部件
    renderAssist() {
        this.clearOffset();
        if (this.data.hasOwnProperty('priority') && this.data.priority) {
            this.priority = new Priority(this.data.priority, this);
            this.add(this.priority);
            this.offset[0] += 24;
            this.data.textPadding[3] += this.offset[0];
            var box = this.text.getBoundingRect();
            this.priority.setPosition(10, (box.height - 16) / 2); //左侧留10px，上下居中
        }
        this.renderText();
    }
    //恢复初始状态
    clearOffset() {
        this.offset = [0, 0]; //用来记录节点左右两侧需要偏移的距离
        if (this.priority) {
            this.remove(this.priority);
            this.priority = null;
            this.data.textPadding[3] -= 24;
        }
    }
    //设置优先级
    setPriority(priority) {
        this.data.priority = priority;
        this.renderAssist();
    }
    removePriority() {
        this.data.priority = '';
        this.renderAssist();
    }
    //覆盖刷新方法
    refresh() {
        this.renderText();
        this.renderAssist();
        if(this.getLevel() == 0 || this.isLeaf()){
            this.expandElement.hide();
        }else{
            this.expandElement.show();
        }
    }

}